/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.platform.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.platform.entity.DeviceTable;
import org.springblade.modules.platform.entity.ProductType;
import org.springblade.modules.platform.mapper.DeviceTableMapper;
import org.springblade.modules.platform.mapper.ProductTypeMapper;
import org.springblade.modules.platform.service.IProductTypeService;
import org.springblade.modules.platform.vo.ProductTypeVO;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2020-11-05
 */
@Service
@Slf4j
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements IProductTypeService {

	@Autowired
	DeviceTableMapper equipmentTypeMapper;

	@Autowired
	ProductTypeMapper productTypeMapper;

	@Override
	public IPage<ProductTypeVO> selectProductTypePage(Query query, ProductType productType) {
		BladeUser user = AuthUtil.getUser();
		boolean admin = AuthUtil.isAdmin();
		log.info("AuthUtil:{}",admin);

		QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
		wrapper.lambda().eq(ProductType::getIsDeleted,0);
		if(!StringUtils.isEmpty(productType.getProductName())){
			wrapper.lambda().like(ProductType::getProductName,productType.getProductName());
		}
		if(productType.getEquipmentType() != null){
			wrapper.lambda().eq(ProductType::getEquipmentType,productType.getEquipmentType());
		}
		if(!StringUtils.isEmpty(productType.getProductId())){
			wrapper.lambda().like(ProductType::getProductId,productType.getProductId());
		}
		/*if(!admin){
			wrapper.lambda().like(ProductType::getCategoryCode,user.getDeptId());
		}*/
		wrapper.lambda().orderByDesc(ProductType::getId);
		IPage<ProductType> page = Condition.getPage(query);
		IPage<ProductType> productTypeIPage = productTypeMapper.selectPage(page, wrapper);


		List<ProductTypeVO> list = new ArrayList<>();
		if(!StringUtils.isEmpty(productTypeIPage.getRecords())){
			productTypeIPage.getRecords().forEach(x->{
				ProductTypeVO productTypeVO = new ProductTypeVO();
				BeanUtils.copyProperties(x,productTypeVO);
				Long integer = equipmentTypeMapper.selectCount(new QueryWrapper<DeviceTable>().lambda()
						.eq(DeviceTable::getProductId, x.getProductId())
						.eq(DeviceTable::getIsDeleted, 0));
				productTypeVO.setEquipmentNumber(integer);
				list.add(productTypeVO);
			});
		}

		IPage<ProductTypeVO> page1 = Condition.getPage(query);
		page1.setRecords(list);
		page1.setTotal(productTypeIPage.getTotal());
		return  page1;
	}

}
